
wptest.logic = function()
{
  return {
    
    msg: function(txt)
    {
      $('messager').empty();
      $('messager').appendText(txt);
    },
    
    init: function()
    {
      $('actLoad').addEvent('click', wptest.logic.actLoadAll);
      $('actSave').addEvent('click', wptest.logic.actSave);
      $('actAuth').addEvent('click', wptest.logic.actAuth);
      $('actClear').addEvent('click', wptest.logic.actClear);
      
    },
    
    actAuth: function()
    {
      var jr = new Request.JSON({
          url: 'auth.php',
          onSuccess: function() {
            wptest.logic.msg('Successfully authenticated. You can now insert and edit data.'); 
          }
      });
      jr.get({});
    },
    
    actClear: function()
    {
      $('id').value = '';
      $('field1').value = '';
      $('field2').value = '';
      $('field3').value = '';
      wptest.logic.msg('Cleared form. Fill out and save for new datasets.');
    },
    
    actLoadAll: function()
    {
      $('datalist').empty();
      
      var t = new wptest.Testers();
      t.load([], '', '', 0, 0, function(t)
      {
        if (t.length() === 0) {
          wptest.logic.msg('No datasets found.');
          return;
        }
        t.rewind();
        var tab = new Element('table', {});
        var tr, td, a;
        $('datalist').grab(tab);
        while (t.findNext()) {
          tr = new Element('tr');
          tr.grab(new Element('td', { html: String(t.get('id'))}));
          tr.grab(new Element('td', { html: String(t.get('field1'))}));
          tr.grab(new Element('td', { html: String(t.get('field2'))}));
          tr.grab(new Element('td', { html: String(t.get('field3'))}));
          td = new Element('td', {});
          a = new Element('a', { href:'#', html: 'show'});
          a.addEvent('click', wptest.logic.actLoadSingle.pass([t.get('id')]));
          td.grab(a);
          tr.grab(td);
          tab.grab(tr);
        }
        wptest.logic.msg(String(t.length())+' datasets loaded.');
      });
    },
    
    actSave: function()
    {
      var a = {
        field1: $('field1').value,
        field2: $('field2').value,
        field3: $('field3').value
      };
      
      var t = new wptest.Tester();
      if ($('id').value !== '') {
        a.id = $('id').value;
        t.update(a, function(t) {
          wptest.logic.msg('Updated dataset');
          wptest.logic.actClear();
          wptest.logic.actLoadAll();
        });
        return;
      }
      
      
      t.qset(a, function(t) {
        t.save(function() {
          wptest.logic.msg('Inserted dataset.');
          wptest.logic.actClear();
          wptest.logic.actLoadAll();
        });
      });
    },
    
    actLoadSingle: function(id)
    {
      var t = new wptest.Tester();
      t.load({id: id}, function(t) {
        $('id').value = t.get('id');
        $('field1').value = t.get('field1');
        $('field2').value = t.get('field2');
        $('field3').value = t.get('field3');        
      });
    }
    
  }
}();



window.addEvent('domready', wptest.logic.init);
